//
//  ZqlRetweetedView.swift
//  ZqlSinaWeiBo
//
//  Created by 赵乾龙 on 16/2/23.
//  Copyright © 2016年 Mr.zhao. All rights reserved.
//

import UIKit
import SnapKit

class ZqlRetweetedView: UIView {
    
    //添加约束对象属性
    var bottomConstraint:Constraint?
    
    var retweetedStatus:ZqlStatusModel?{
        didSet{
            retweetedLabel.text = retweetedStatus?.text
            pictureView.imageUrls = retweetedStatus?.imageUrls
            
            //卸载老的约束对象
            self.bottomConstraint?.uninstall()
              //根据数据调整约束关系
            if let urls = retweetedStatus?.imageUrls where urls.count != 0 {
                // 转发微博有配图视图
                self.snp_makeConstraints { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(cellMargin).constraint
                }
            } else {
                //转发微博没有配图视图
                self.snp_makeConstraints { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(retweetedLabel.snp_bottom).offset(cellMargin).constraint
                }
            }

        }
    }

    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor(white: 0.95, alpha: 1)
        setupUI()
    }
    
    //默认实现报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置界面
    private func setupUI() {
        addSubview(retweetedLabel)
        addSubview(pictureView)
        
        //设置约束
        retweetedLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top).offset(cellMargin)
            make.left.equalTo(self.snp_left).offset(cellMargin)
            make.right.equalTo(self.snp_right).offset(-cellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetedLabel.snp_bottom).offset(cellMargin)
            make.left.equalTo(retweetedLabel.snp_left)
        }
        
        //转发微博的地步需要添加约束
        self.snp_makeConstraints { (make) -> Void in
          self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(cellMargin).constraint
        }
    }
    
    //懒加载视图
    private lazy var retweetedLabel: UILabel = UILabel(text: "转发微博:终于等到你", fontSize: 14, textColor: UIColor.darkGrayColor(), alignment: .Left)
    

    //配图视图
    private lazy var pictureView: ZqlPictureView = ZqlPictureView()

    
    
}
